<template>
  <transition name="fade">
    <div v-if="loading" class="global-loading">
      <div class="loading-content">
        <el-icon class="loading-icon" :size="32"><Loading /></el-icon>
        <span class="loading-text">{{ text }}</span>
      </div>
    </div>
  </transition>
</template>

<script setup lang="ts">
import { Loading } from '@element-plus/icons-vue'
import { useLoadingStore } from '@/stores/loading'
import { storeToRefs } from 'pinia'

const loadingStore = useLoadingStore()
const { loading } = storeToRefs(loadingStore)

withDefaults(defineProps<{
  text?: string
}>(), {
  text: '加载中...'
})
</script>

<style scoped lang="scss">
.global-loading {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: rgba(255, 255, 255, 0.9);
  z-index: 2000;
  display: flex;
  justify-content: center;
  align-items: center;

  .loading-content {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 10px;
  }

  .loading-icon {
    animation: rotating 2s linear infinite;
    color: #409eff;
  }

  .loading-text {
    color: #606266;
    font-size: 14px;
  }
}

@keyframes rotating {
  from {
    transform: rotate(0deg);
  }
  to {
    transform: rotate(360deg);
  }
}

.fade-enter-active,
.fade-leave-active {
  transition: opacity 0.3s ease;
}

.fade-enter-from,
.fade-leave-to {
  opacity: 0;
}
</style> 